//
// Copyright (c) 2024 BPer
//
#include <bpersdp/BPerMediaPayloadType.h>
#include <bperutil/sys/BPerCString.h>

#define kCRLF  "\r\n"

bPer_vector_impl(BPerMediaPayloadType)

int32_t bPer_encode_mediapayloadtype(BPerMediaPayloadType *payload,
		BPerBuffer *os) {
	int32_t err = BPer_Ok;
	char intstr[32];
	bPer_memset(intstr, 0, sizeof(intstr));
	bPer_itoa(payload->payload_type, intstr, 10);
	bPer_write_cstring(os, "a=rtpmap:");
	bPer_write_cstring(os, intstr);
	bPer_write_cstring(os, " ");
	bPer_write_cstring(os, payload->encoding_name);
	bPer_write_cstring(os, "/");
	bPer_memset(intstr, 0, sizeof(intstr));
	bPer_itoa(payload->clock_rate, intstr, 10);
	bPer_write_cstring(os, intstr);

	if (bPer_strlen(payload->encoding_param)) {
		bPer_write_cstring(os, "/");
		bPer_write_cstring(os, payload->encoding_param);

	}
	bPer_write_cstring(os, kCRLF);



	for (int32_t i = 0; i < payload->rtcp_fb.vsize; i++) {
		bPer_memset(intstr, 0, sizeof(intstr));
		bPer_itoa(payload->payload_type, intstr, 10);
		//bPer_write_cstring(os, intstr);
		bPer_write_cstring(os, "a=rtcp-fb:");
		bPer_write_cstring(os, intstr);
		bPer_write_cstring(os, " ");
		bPer_write_cstring(os, payload->rtcp_fb.payload[i]);
		bPer_write_cstring(os, kCRLF);
	}
	if (bPer_strlen(payload->format_specific_param)) {
		bPer_write_cstring(os, "a=fmtp:");
		bPer_memset(intstr, 0, sizeof(intstr));
		bPer_itoa(payload->payload_type, intstr, 10);
		bPer_write_cstring(os, intstr);
		bPer_write_cstring(os, " ");
		bPer_write_cstring(os, payload->format_specific_param);
		bPer_write_cstring(os, kCRLF);


	}

	return err;
}

